package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.exception.ValidateException;
import org.light.utils.DomainUtil;
import org.light.utils.WriteableUtil;

public class DateTimeUtilGenerator extends Generator{
	protected Domain sampleDomain;

	public DateTimeUtilGenerator(Domain sampleDomain){
		super();
		super.fileName = "DateTimeUtil.go";
		super.standardName = "DateTimeUtil";
		this.sampleDomain = sampleDomain;
	}

	@Override
	public StatementList generateStatementList() throws ValidateException{
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package utils"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import ("));
		sList.add(new Statement(4000L,1,"\""+DomainUtil.packagetokenToGoPackageFolder(this.sampleDomain.getProjectName(),this.sampleDomain.getPackageToken()+"."+this.sampleDomain.getDomainSuffix())+"\""));
		sList.add(new Statement(5000L,1,"\"fmt\""));
		sList.add(new Statement(6000L,1,"\"time\""));
		sList.add(new Statement(7000L,1,"\"database/sql\""));
		sList.add(new Statement(8000L,0,")"));
		sList.add(new Statement(9000L,0,""));
		sList.add(new Statement(10000L,0,"func ParseStringToDate(dateString string) "+this.sampleDomain.getDomainSuffix()+".Date {"));
		sList.add(new Statement(11000L,1,"if dateString == \"\" {"));
		sList.add(new Statement(12000L,2,"return  "+this.sampleDomain.getDomainSuffix()+".Date{ Time: time.Time{}, Valid:false}"));
		sList.add(new Statement(13000L,1,"} else {"));
		sList.add(new Statement(14000L,2,"fmt.Println(dateString)"));
		sList.add(new Statement(15000L,2,"tm, _ := time.Parse("+this.sampleDomain.getDomainSuffix()+".DateFormart, dateString)"));
		sList.add(new Statement(16000L,2,"return "+this.sampleDomain.getDomainSuffix()+".Date{ Time: tm, Valid:true}"));
		sList.add(new Statement(17000L,1,"}"));
		sList.add(new Statement(18000L,0,"}"));
		sList.add(new Statement(19000L,0,""));
		sList.add(new Statement(20000L,0,"func ParseStringToDateTime(dateTimeString string) "+this.sampleDomain.getDomainSuffix()+".DateTime {"));
		sList.add(new Statement(21000L,1,"if dateTimeString == \"\" {"));
		sList.add(new Statement(22000L,2,"return  "+this.sampleDomain.getDomainSuffix()+".DateTime{ Time: time.Time{}, Valid:false}"));
		sList.add(new Statement(23000L,1,"} else {"));
		sList.add(new Statement(24000L,2,"tm, _ := time.Parse("+this.sampleDomain.getDomainSuffix()+".DateTimeFormart, dateTimeString)"));
		sList.add(new Statement(25000L,2,"return "+this.sampleDomain.getDomainSuffix()+".DateTime{ Time: tm, Valid:true}"));
		sList.add(new Statement(26000L,1,"}"));
		sList.add(new Statement(27000L,0,"}"));
		sList.add(new Statement(28000L,0,""));
		sList.add(new Statement(29000L,0,"func ParseNullTimeToDate(value sql.NullTime) "+this.sampleDomain.getDomainSuffix()+".Date {"));
		sList.add(new Statement(30000L,1,"if value.Valid {"));
		sList.add(new Statement(31000L,2,"return "+this.sampleDomain.getDomainSuffix()+".Date{Time:value.Time, Valid: true}"));
		sList.add(new Statement(32000L,1,"} else {"));
		sList.add(new Statement(33000L,2,"return  "+this.sampleDomain.getDomainSuffix()+".Date{ Time: time.Time{}, Valid:false}"));
		sList.add(new Statement(34000L,1,"}"));
		sList.add(new Statement(35000L,0,"}"));
		sList.add(new Statement(36000L,0,""));
		sList.add(new Statement(37000L,0,"func ParseNullTimeToDateTime(value sql.NullTime) "+this.sampleDomain.getDomainSuffix()+".DateTime {"));
		sList.add(new Statement(38000L,1,"if value.Valid {"));
		sList.add(new Statement(39000L,2,"return "+this.sampleDomain.getDomainSuffix()+".DateTime{Time:value.Time, Valid:true}"));
		sList.add(new Statement(40000L,1,"} else {"));
		sList.add(new Statement(41000L,2,"return  "+this.sampleDomain.getDomainSuffix()+".DateTime{ Time: time.Time{}, Valid:false}"));
		sList.add(new Statement(42000L,1,"}"));
		sList.add(new Statement(43000L,0,"}"));

		return WriteableUtil.merge(sList);
	}

	public Domain getSampleDomain() {
		return sampleDomain;
	}

	public void setSampleDomain(Domain sampleDomain) {
		this.sampleDomain = sampleDomain;
	}

}
